package com.maoatao.easycodegen.core;

import com.baomidou.mybatisplus.annotation.IdType;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * 代码生成引擎
 *
 * @author MaoAtao
 * @date 2025-10-04
 */
public class CodeGen {

    private static final Logger log = LoggerFactory.getLogger(CodeGen.class);

    public static final GeneratorConfig GLOBAL_CONFIG = new GeneratorConfig();

    public static final CodeGenerator GENERATOR = new CodeGenerator();

    static {
        // 输出路径

        // web
        GLOBAL_CONFIG.setWebOutputPath("src/test");
        // model
        GLOBAL_CONFIG.setModelOutputPath("src/test");
        // service
        GLOBAL_CONFIG.setServiceOutputPath("src/test");
        // dao
        GLOBAL_CONFIG.setDaoOutputPath("src/test");

        // 生成类配置

        // 表前缀
        GLOBAL_CONFIG.setTablePrefix("t_");
        // 包名
        GLOBAL_CONFIG.setPackageName("com.maoatao.test");
        // 作者
        GLOBAL_CONFIG.setAuthorName("CodeGeneratorTest");

        // restful 风格

        // 连字 HYPHENATED -> /api/demo/user-infos
        // 层级 GRADATION -> /api/demo/user/infos
        // 驼峰 CAMEL_CASE -> /api/demo/userInfos
        GLOBAL_CONFIG.setUrlStyle(UrlStyle.HYPHENATED);

        // 主键生成策略(默认使用mybatisplus实现的雪花算法)
        GLOBAL_CONFIG.setIdType(IdType.ASSIGN_ID);

        // 数据库字段类型 -> java类型 映射表

        // Boolean
        GLOBAL_CONFIG.putFieldTypeMapping("tinyint", "Boolean");
        GLOBAL_CONFIG.putFieldTypeMapping("bit", "Boolean");

        // Integer
        GLOBAL_CONFIG.putFieldTypeMapping("smallint", "Integer");
        GLOBAL_CONFIG.putFieldTypeMapping("mediumint", "Integer");
        GLOBAL_CONFIG.putFieldTypeMapping("int", "Integer");
        GLOBAL_CONFIG.putFieldTypeMapping("integer", "Integer");

        // Long
        GLOBAL_CONFIG.putFieldTypeMapping("bigint", "Long");

        // Float
        GLOBAL_CONFIG.putFieldTypeMapping("float", "Float");

        // Double
        GLOBAL_CONFIG.putFieldTypeMapping("double", "Double");

        // BigDecimal
        GLOBAL_CONFIG.putFieldTypeMapping("decimal", "BigDecimal");

        // String
        GLOBAL_CONFIG.putFieldTypeMapping("char", "String");
        GLOBAL_CONFIG.putFieldTypeMapping("varchar", "String");
        GLOBAL_CONFIG.putFieldTypeMapping("tinytext", "String");
        GLOBAL_CONFIG.putFieldTypeMapping("text", "String");
        GLOBAL_CONFIG.putFieldTypeMapping("mediumtext", "String");
        GLOBAL_CONFIG.putFieldTypeMapping("longtext", "String");

        // LocalDateTime
        GLOBAL_CONFIG.putFieldTypeMapping("date", "LocalDateTime");
        GLOBAL_CONFIG.putFieldTypeMapping("datetime", "LocalDateTime");
        GLOBAL_CONFIG.putFieldTypeMapping("timestamp", "LocalDateTime");
    }

    public static void generate(String... ddls) {
        if (ddls == null || ddls.length == 0) {
            throw new IllegalArgumentException("ddls is null!");
        }
        for (int i = 0; i < ddls.length; i++) {
            String s = ddls[i];
            if (s == null || s.trim().isEmpty()) {
                throw new IllegalArgumentException("ddls[" + i + "] is null!");
            }

            GLOBAL_CONFIG.setDdl(s);
            GENERATOR.generate(GLOBAL_CONFIG);
            String text = """
                    
                    -------------------------------------
                    
                    -- Generate code for DDL
                    {}
                    
                    -------------------------------------
                    
                    """;
            log.info(text, s);
        }
    }
}
